package com.matrix.playelasticsearch.controller;

import com.matrix.playelasticsearch.dao.ECommerceRepository;
import com.matrix.playelasticsearch.enity.es.ECommerce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ecommerce")
public class ECommerceController {

    @Autowired
    ECommerceRepository eCommerceRepository;

    @RequestMapping("/findByCategory")
    Page<ECommerce> findByCategory(@RequestParam String category,
                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        Page<ECommerce> eCommerces = eCommerceRepository.findByCategory(category, PageRequest.of(pageNum - 1, pageSize));
        return eCommerces;
    }

}
